package com.ybkj.o2o.zhuanche.manager.impl;

import com.ybkj.o2o.zhuanche.common.DataGrid;
import com.ybkj.o2o.zhuanche.common.query.InsuranceQuery;
import com.ybkj.o2o.zhuanche.entity.Car;
import com.ybkj.o2o.zhuanche.entity.Insurance;
import com.ybkj.o2o.zhuanche.manager.InsuranceManager;
import com.ybkj.o2o.zhuanche.service.CarService;
import com.ybkj.o2o.zhuanche.service.InsuranceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DefaultInsuranceManager implements InsuranceManager {

	@Autowired
	private InsuranceService insuranceService;
	@Autowired
	private CarService carService;
	@Override
	public int save(Insurance insurance) {
		return insuranceService.save(insurance);
	}
	@Override
	public DataGrid<Insurance> finfInsuranceByCarId(Long carId, String appKey,
			Integer page, Integer rows) {
		InsuranceQuery insuranceQuery = new InsuranceQuery();
		insuranceQuery.setCarId(carId);
		insuranceQuery.setAppKey(appKey);
		insuranceQuery.setPage(page);
		insuranceQuery.setRows(rows);
		Car car = carService.findByCarId(carId, appKey);
		List<Insurance> insurances = insuranceService.selectByCarId(insuranceQuery);
		for(Insurance ins : insurances){
			if(car == null){
				continue;
			}
			ins.setCertificateNo(car.getTaxiNo());
			ins.setVehicleLicense(car.getCarNumber());
		}
		Long total = insuranceService.countByCarId(insuranceQuery);
		DataGrid<Insurance> data = new DataGrid<>();
		data.setRows(insurances);
		data.setTotal(total);
		return data;
	}

	@Override
	public Insurance deleteById(Long id, String appKey) {
		Insurance insurance = insuranceService.findById(id,appKey);
		insuranceService.deleteById(id,appKey);
		return insurance;
	}

}
